/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.util;

import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

/**
 *
 * @author Jakub Kutil
 */
public class DbConnector {

    private static GraphDatabaseService graphDb;
    private static ExecutionEngine executionEngine;
    /**
     * Connects to DB
     *
     * @return DB connection
     */
    public static GraphDatabaseService getDBConnection() {

        if(graphDb == null){    // lokaci je nutné změnit
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("C:\\Users\\Kuba\\Desktop\\diplomka\\MC");
        registerShutdownHook(graphDb);
        }
        return graphDb;
    }

    public static ExecutionEngine getEngine() {

        if(executionEngine == null){
        executionEngine = new ExecutionEngine(graphDb);
        }
        return executionEngine;
    }
    
    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                graphDb.shutdown();
            }
        });
    }
}
